// 'user strict';
'use strict';
const Controller = require('egg').Controller;
class JspangController extends Controller {

  /* new */
  // ----------------------------------

  // test Helper extension
  async baseHelper() {
    const { ctx } = this;
    // ctx.response.token = 'jspang_base64Helper'
    const testBase64 = ctx.helper.base64Encode('jspang_base64Helper')
    ctx.body = testBase64
  }

  //test Response token setter:
  async newResponse() {
    const { ctx } = this;
    ctx.response.token = 'jspang_by Setter_ ' + `${new Date()}`
    // const tk = ctx.response.token + "😎"
    // console.log(tk); undefined!
    // to check the result of the token setter,you may need to open /newResponse with corresponding port ,and use the devTool,open the `network`panel ,the refresh the page to give the change for it to capture response information
    ctx.body = 'newRespose:test token setter'
    // ctx.body += "😎test append text";//that is ok, the brower will render the final value of the `ctx.body`
  }
  // test Request token getter.
  async newRequest() {
    const { ctx } = this;
    const token = ctx.request.token
    ctx.body = {
      status: 200,
      body: token
    }

  }

  // test Context extensions:
  // test get:
  async newContextGet() {
    const { ctx } = this;
    const params = ctx.params();
    console.log(params)
    ctx.body = 'newContextGet';
  }
  // test post:
  async newContextPost() {
    const { ctx } = this;
    const params = ctx.params();
    console.log(params)
    ctx.body = 'newContextPost';
  }
  /*   async newContext() {
      const { ctx } = this;
      ctx.body = 'newContext';
    } */

  async indexPage() {
    /* 从this中解构出app对象 */
    const { ctx, app } = this;
    // const { ctx } = this;

    /* 😎new: */
    // test the middleware(`counter`) by console.log
    console.log(ctx.session.counter)
    //获取Session
    const username = ctx.session.username
    await ctx.render(
      // param1:(从指定的html文件渲染出前端页面)
      'jspang.html',
      // param2:
      {
        // 演示application 扩展
        // 方法扩展:
        nowTimeMethod: app.currentTime(),
        // 扩展属性
        nowTimeProperty: app.timeProperty,
        id: 2021,
        name: "ψ(｀∇´)ψ人无再少年",
        age: 18,
        // 演示session
        //赋值给模板
        username: username,
        // 演示列表渲染:
        skills: [
          'sleeping',
          'eating',
          '🍝 '
        ],
        skill: "massage"
      });

    /* old */
    /* try unit test: */
    // ctx.body = 'hi,egg'
    // ctx.body = '<h1> I am Jspang</h1 >';
  }

  async add() {
    const ctx = this.ctx
    /* 测试多种cookie.set()(可以指定三个参数) */
    /* 加密后的cookie可以通过为show方法添加解密参数来从服务端获取解密内容
    注意,第三个参数需要以对象的形式编写 */
    ctx.cookies.set(
      "user",
      "测试中文cookie(添加encrypt参数以支持中文)",
      { encrypt: true }
    )
    /* 使用session来保存重要信息: */
    ctx.session.username = 'jspang'
    /*     ctx.cookies.set(
          "user",
          "jspang.com",
          {
            // cookies 自动删除(值为毫秒数)
            maxAge: 1000 * 3,
            // 是否只允许通过服务端操作cookie
            // 为了安全起见,一般设置为true ;如果设置为false,则可以通过浏览器document.cookie获取cookie
            httpOnly: true
    
          }
        ) */

    /* body */
    ctx.body = {
      status: 200,
      data: 'Cookie添加成功'
    }
  }
  /*   async add() {
      const ctx = this.ctx
      ctx.cookies.set("user", "jspang.com")
      ctx.body = {
        status: 200,
        data: 'Cookie添加成功'
      }
    } */
  async del() {
    const ctx = this.ctx
    //演示session的删除
    ctx.session.username = null;
    ctx.cookies.set("user", null)
    ctx.body = {
      status: 200,
      data: 'Cookie删除成功'
    }
  }
  /* modifier: */
  async editor() {
    const ctx = this.ctx
    ctx.cookies.set("user", 'bilibili')
    // 演示session(本身支持中文)的修改
    ctx.session.username = '技术胖最帅'
    ctx.body = {
      status: 200,
      data: 'Cookie修改成功'
    }
  }
  async show() {
    const ctx = this.ctx
    /* 可以为cookies.get()传入解密参数选项,来获取中解密后的结果 */
    const user = ctx.cookies.get(
      "user",
      { encrypt: true }
    )
    console.log(user)
    ctx.body = {
      status: 200,
      data: 'Cookie显示成功'
    }
  }
  /* to use egg-view-ejs to show/render the html  */
  async index() {
    // there ctx is assigned by this
    const { ctx } = this;
    await ctx.render(

      // param1:
      'jspang.html',
      // param2:
      {
        id: 2021,
        name: "ψ(｀∇´)ψ人无再少年",
        age: 18,
        skill: "massage"
      });
    /* old */
    // await ctx.render('jspang.html');

  }
  /* post request mode */
  async addPost() {
    const ctx = this.ctx;

    ctx.body = {
      status: 200,
      data: ctx.request.body
    }
    // ctx.body = "add page";
  }

  /* set for strict parameter pass mode */
  async getGirl2() {
    const ctx = this.ctx;
    ctx.body = 'getGirl2:' + "name:" + ctx.params.name + "," + "age:" + ctx.params.age
  }

  // set for :free to pass parameters:

  async getGirl() {
    const ctx = this.ctx;
    const res = await ctx.service.jspang.getGirl("1111")
    ctx.body = res
    // ctx.body = ctx.query
  }

  /* old */

  async getGirls() {
    const { ctx } = this;
    await new Promise(
      resolve => {
        setTimeout(() => {
          resolve(ctx.body = '<h1>a is coming to you</h1>');
        }, 1000);
        console.log(ctx.body);
      }
    );
  }

}
module.exports = JspangController;
